package gew.table;

import gew.metadata.CustomSql;
import gew.system.Constants;
import gew.utils.SqlUtils;
import java.util.ArrayList;
import java.util.List;

public class TableDimension {
    private final String factTableField;
    private final Integer hierGroupId;
    private final String title;
    private final String ids;
    private final boolean showInTable;
    private final boolean showInBalloon;
    private final boolean showCode;
    private final String css;
    private final ArrayList<Integer> linkTypes;    
    private final ArrayList<Integer> selectables; 
    private final boolean noParents;
    private ArrayList<TableDimensionValue> values;
    private final int index;
    private ArrayList<Integer> iterator;

    TableDimension(int index, int hierGroupId, String factTableField, String title, String ids, boolean showInTable, boolean showInBalloon, String css, boolean showCode, boolean noParents) {
        this.hierGroupId = hierGroupId;
        this.factTableField = factTableField;
        this.title = title;
        this.ids = ids;
        this.linkTypes = new ArrayList<>();
        this.linkTypes.add(Constants.HIER_LINK_TYPE_PARENTS);
        this.selectables = new ArrayList<>();
        this.selectables.add(1);
        this.noParents = noParents;
        this.index = index;
        this.showInTable = showInTable;
        this.showInBalloon = showInBalloon;
        this.css = css;
        this.showCode = showCode;
    }

    TableDimension(int index, int hierGroupId, String factTableField, String title, String ids, boolean showInTable, boolean showInBalloon, String css, boolean showCode) {
        this(index, hierGroupId, factTableField, title, ids, showInTable, showInBalloon, css, showCode, false);
    }

    public String getFactTableField() {
        return factTableField;
    }

    public String getTitle() {
        return title;
    }

    private String getLinkTypesStr() {
        if (linkTypes.isEmpty())
            return "0";
        else{
            String r = linkTypes.get(0).toString();
            for (int i = 1; i < linkTypes.size(); i++){
                r = r.concat(SqlUtils.COMMA).concat(linkTypes.get(i).toString());
            }
            return r;
        }
    }

    private String getSelectablesStr() {
        if (selectables.isEmpty())
            return "0";
        else{
            String r = selectables.get(0).toString();
            for (int i = 1; i < selectables.size(); i++){
                r = r.concat(SqlUtils.COMMA).concat(selectables.get(i).toString());
            }
            return r;
        }
    }
    
    String getLoadSql(String locale){
        return CustomSql.getTableDimension(locale, ids, hierGroupId.toString(), getLinkTypesStr(), getSelectablesStr(), noParents);
    }
    
    public List<TableDimensionValue> getValues(){
        return values;
    }
    
    void setValues(ArrayList<TableDimensionValue> values){
        this.values = values;
        for (int i = 0; i < this.values.size(); i++){
            this.values.get(i).setTableDimension(this);
        }
    }
    
    void setColspan(int colspan){
        for (int i = 0; i < values.size(); i++){
            values.get(i).setColspan(colspan);
        }
    }
    
    void setRowspan(int rowspan){
        for (int i = 0; i < values.size(); i++){
            values.get(i).setRowspan(rowspan);
        }
    }

    public Integer getIndex() {
        return index;
    }

    void setIterateCount(int iterateCount) {
        iterator = new ArrayList<>();
        for (int i = 0; i < iterateCount; i++){
            iterator.add(i);
        }
    }

    public Iterable<Integer> getIterator() {
        return iterator;
    }

    public boolean isShowInTable() {
        return showInTable;
    }

    public boolean isShowInBalloon() {
        return showInBalloon;
    }

    String getLadedValuesIdsCS() {
        String r = SqlUtils.EMPTY;
        for (int i = 0; i < this.values.size(); i++){
            r = r
                    .concat(i > 0 ? SqlUtils.COMMA : SqlUtils.EMPTY)
                    .concat(values.get(i).getId().toString());
        }
        if (r.isEmpty()) {
            r = "-1";
        }
        return r;
    }

    public String getCss() {
        return css;
    }

    public boolean isShowCode() {
        return showCode;
    }
    
}
